package lucee.runtime.functions.system;

import java.util.Iterator;

import lucee.runtime.PageContext;
import lucee.runtime.exp.FunctionException;
import lucee.runtime.exp.PageException;
import lucee.runtime.ext.function.BIF;
import lucee.runtime.op.Caster;
import lucee.runtime.type.Array;
import lucee.runtime.type.ArrayImpl;
import lucee.runtime.type.Struct;
import lucee.runtime.type.util.KeyConstants;

public final class LuceeVersionsList extends BIF {

	private static final long serialVersionUID = -2983771649902173955L;

	public static Array call(PageContext pc, String type) throws PageException {
		try {
			return LuceeVersionsListMvn.invoke("LuceeVersionsList", pc, type);
		}
		catch (Exception e) {

		}
		Array data = LuceeVersionsListS3.call(pc, type);
		Array arr = new ArrayImpl();
		Struct sct;
		Iterator<Object> it = data.valueIterator();
		while (it.hasNext()) {
			sct = Caster.toStruct(it.next());
			arr.append(sct.get(KeyConstants._version));
		}
		return arr;
	}

	@Override
	public Object invoke(PageContext pc, Object[] args) throws PageException {
		if (args.length == 1) return call(pc, Caster.toString(args[0]));
		if (args.length == 0) return call(pc, null);

		throw new FunctionException(pc, "LuceeVersionsListS3", 0, 1, args.length);
	}
}